<?php
//模型
class CourseTrainModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'course_train';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " WHERE 1 ";
		if(isset($arr['user_id']) && !empty($arr['user_id'])){
			$where .= " AND t.user_id='{$arr['user_id']}' ";
		}
		if(isset($arr['coach_id']) && !empty($arr['coach_id'])){
			$where .= " AND t.coach_id='{$arr['coach_id']}' ";
		}
		
		$sql  = "SELECT t.*, c.nickname as coach_name, c.headimg as coach_headimg from ".
				$this->db->dbprefix.$this->table." AS t LEFT JOIN ".
				$this->db->dbprefix."coach AS c ON c.id=t.coach_id 				
			    {$where} ORDER BY t.addtime DESC LIMIT ".($page-1)*$size .",{$size}";
				
		$query = $this->db->query($sql);
		$res = $query->result_array();		

		$sql  = "SELECT count(*) AS num from ".$this->db->dbprefix.$this->table." AS t {$where}";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists(){		
		$sql  = "select * from ".$this->db->dbprefix.$this->table;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		return $res;		
	}
	//详情
	function get_info($id,$user_id=0){
		$sql  = "SELECT t.*, c.nickname as coach_name, c.headimg as coach_headimg from ".
				$this->db->dbprefix.$this->table." AS t LEFT JOIN ".
				$this->db->dbprefix."coach AS c ON c.id=t.coach_id 
				where t.id='{$id}' and t.user_id='{$user_id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		if(!$row){return false;}
		
		
		$row['addtime'] = date("Y-m-d H:i:s",$row['addtime']);
		
		/*$sport = array();
		if($row['sport']){
			$this->load->model('Index/SportModel','sport','index');			
			$sport_id = explode(',',$row['sport']);
			foreach($sport_id as $v){
				$ls_arr = $this->sport->get_info($v);					
				$sport[] = $ls_arr;
			}
		}
		$row['sport_list'] = $sport;*/
		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

